const ENV = import.meta.env

let config = {}

const configSource = {
  appCode: ENV.VITE_APP_CODE,
  projectCode: `${ENV.VITE_APP_CODE}_${ENV.VITE_APP_ENV}`,
  projectName: ENV.VITE_APP_NAME,
  projectDesc: ENV.VITE_APP_DESCRIPTION,
  base: ENV.VITE_BASE,
  baseUrl: ENV.VITE_BASE_URL,
  mockBaseUrl: ENV.BASE_MOCK_URL,
  serverUrl: ENV.VITE_BASE_SERVER_URL,
  github: 'https://github.com/isboyjc/toolsdog',
  icp: '京ICP备2022xxxxxxx号',
  me: {
    name: 'isboyjc',
    gzhName: '不正经的前端',
    gzhUrl: 'http://qiniuimages.isboyjc.com/picgo/202210030159449.jpeg',
    github: 'https://github.com/isboyjc'
  }
}

const setConfig = cfg => {
  config = Object.assign(config, cfg)
  return config
}

const resetConfig = () => {
  config = { ...configSource }
  return config
}

resetConfig()

const getConfig = key => {
  if (typeof key === 'string') {
    const arr = key.split('.')
    if (arr && arr.length) {
      let data = config
      arr.forEach(v => {
        if (data && typeof data[v] !== 'undefined') {
          data = data[v]
        } else {
          data = null
        }
      })
      return data
    }
  }
  if (Array.isArray(key)) {
    const data = config
    if (key && key.length > 1) {
      let res = {}
      key.forEach(v => {
        if (data && typeof data[v] !== 'undefined') {
          res[v] = data[v]
        } else {
          res[v] = null
        }
      })
      return res
    }
    return data[key]
  }
  return { ...config }
}

export { getConfig, setConfig, resetConfig }
